//
// Description: 899. 编辑距离
// Created by Loading on 2022/6/3.
//

#include <bits/stdc++.h>

using namespace std;

constexpr int N = 15, M = 1010;

int n, m;
// 储存已有字符串
char s[M][N];
// dp[i][j] 表示 a 的前 i 个字符变成 b 的前 j 个字符所需要的最少操作次数
int dp[N][N];

int edit_distance(char a[], char b[]) {
    // 注意取长度从 a + 1 和 b + 1 开始
    int la = strlen(a + 1), lb = strlen(b + 1);
    // 初始化
    for (int i = 1; i <= la; ++i) {
        dp[i][0] = i;
    }
    for (int j = 1; j <= lb; ++j) {
        dp[0][j] = j;
    }

    // 状态转移
    for (int i = 1; i <= la; ++i) {
        for (int j = 1; j <= lb; ++j) {
            dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + 1;
            // 如果 a[i] == b[j]，则无需操作
            dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + (int) (a[i] != b[j]));
        }
    }

    return dp[la][lb];
}

int main() {
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        cin >> (s[i] + 1);
    }

    while (m--) {
        char a[15];
        int limit;
        cin >> (a + 1) >> limit;

        int cnt = 0;
        for (int i = 0; i < n; ++i) {
            if (edit_distance(s[i], a) <= limit) {
                ++cnt;
            }
        }

        cout << cnt << endl;
    }

    return 0;
}